package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.pojo.User;

import com.itany.service.UserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 类的描述
 * Author: SJY
 * Date: 2021/08/26 10:15
 * Version: 1.0 版本信息
 */

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam(defaultValue="1")Integer page,
                                      @RequestParam(defaultValue="10")Integer rows,
                                      String username,Integer flag){
        Map<String,Object> map = new HashMap<String, Object>();
        User user = new User();
        if(username!=null && username!=""){
            user.setUsername(username);
        }
        if(flag!=null && flag!=-1){
            user.setFlag(flag);
        }
        PageInfo<User> info = userService.findUserAll(page, rows,user);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public ActionResult updateUser(Integer id,String username,String phone,String email,String interest,String password,Integer flag){
        User u = new User(id,username,phone,email,interest,password,flag);
        ActionResult at = userService.updateUser(u);
        return at;
    }

    @RequestMapping("/updateToOpen")
    @ResponseBody
    public ActionResult updateToOpen(Integer id,Integer flag){
        ActionResult ar = new ActionResult();
        if(flag==1){
            return ar;
        }
        User u = new User();
        u.setId(id);
        u.setFlag(1);
        userService.updateUser(u);
        return ar;
    }

    @RequestMapping("/updateToClose")
    @ResponseBody
    public ActionResult updateToClose(Integer id,Integer flag){
        ActionResult ar = new ActionResult();
        if(flag!=1){
            return ar;
        }
        User u = new User();
        u.setId(id);
        u.setFlag(2);
        userService.updateUser(u);
        return ar;
    }

}
